#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int svm_test()
{
  int width=512, heigth=512;
  cv::Mat image = cv::Mat::zeros(width, heigth, CV_8UC3);
  float labels[4] = { 1, -1, -1, -1};
  cv::Mat labelsMat(4, 1, CV_32FC1, labels);
  
  float trainingData[4][2] = { {255, 255}, {10, 10}, {50, 25}, {10, 120} };
  cv::Mat trainingDataMat(4, 2, CV_32FC1, trainingData);
  
 
   CvSVMParams params; 
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
    
    CvSVM SVM; 
    SVM.train(trainingDataMat, labelsMat, cv::Mat(), cv::Mat(), params);
  
    cv::Vec3b green(0,255,0), blue (255,0,0);
    // Show the decision regions given by the SVM
    for (int i = 0; i < image.rows; ++i)
        for (int j = 0; j < image.cols; ++j)
        {
            cv::Mat sampleMat = (cv::Mat_<float>(1,2) << i,j);
            float response = SVM.predict(sampleMat);

            if (response == 1)
                image.at<cv::Vec3b>(j, i)  = green;
            else if (response == -1)
                 image.at<cv::Vec3b>(j, i)  = blue;
        }
        
      int thickness = -1;
    int lineType = 8;
    cv::circle( image, cv::Point(255,  255), 5, cv::Scalar(  0,   0,   0), thickness, lineType);
    cv::circle( image, cv::Point(10,  10), 5, cv::Scalar(255, 255, 255), thickness, lineType);
    cv::circle( image, cv::Point(50, 25), 5, cv::Scalar(255, 255, 255), thickness, lineType);
    cv::circle( image, cv::Point( 10, 120), 5, cv::Scalar(255, 255, 255), thickness, lineType);

    // Show support vectors
    thickness = 2;
    lineType  = 8;
    int c     = SVM.get_support_vector_count();

    for (int i = 0; i < c; ++i)
    {
        const float* v = SVM.get_support_vector(i);
        circle( image,  cv::Point( (int) v[0], (int) v[1]),   6,  cv::Scalar(128, 128, 128), thickness, lineType);
    }

    //imwrite("result.png", image);        // save the image
    cv::namedWindow("SVM Simple Example", CV_WINDOW_NORMAL);
    cv::imshow("SVM Simple Example", image); // show it to the user
    cv::waitKey(0);
    
}

int haartraining_test(cv::Mat image)
{
      cv::CascadeClassifier classifier("haarcascade_eye.xml");
      //if(classifier.empty() ) return -1;
	cv::Mat gimage;
      cv::cvtColor(image, gimage, CV_BGR2RGB);
      
      std::vector<cv::Rect> objects;
      classifier.detectMultiScale(gimage, objects);
      
      for(std::vector<cv::Rect>::iterator it= objects.begin(); it != objects.end(); ++it)
      {
	cv::Point center( it->x + it->width*0.5, it->y + it->height*0.5 );
       int radius = (it->width + it->height)*0.25;
       cv::circle( image, center, radius, cv::Scalar( 255, 0, 0 ), 4, 8, 0 );
      }
      cv::string window = "haartraining testing";
      cv::namedWindow(window, CV_WINDOW_NORMAL);
      cv::imshow(window, image); 
	
      cv::waitKey();
}